import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jxchmz/%E4%BB%93%E7%AE%A1/%E7%89%A9%E6%96%99%E7%B1%BB%E5%88%AB%E7%AE%A1%E7%90%86/MaterialTypeManageList.dart';
import 'package:jxchmz/%E7%99%BB%E5%BD%95/Login.dart';
import 'package:jxchmz/%E8%93%9D%E7%89%99/BleManager.dart';
import 'package:jxchmz/%E9%94%80%E5%94%AE/%E4%BC%9A%E5%91%98%E7%99%BB%E8%AE%B0/VipList.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';

class UserCenter extends StatefulWidget {
  @override
  _UserCenterState createState() => new _UserCenterState();
}

class _UserCenterState extends State<UserCenter> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true; //要点2
  //本地缓存
  GetStorage getStorage = new GetStorage();

  //权限标识 0销售  1仓管  2店长
  var permissions = 1;

  //用户姓名
  var nickName = "";
var IsConnect = false;

  //月订单
  int listMonthNum=0;
  //月收入
  double listMonthCount=0;
  //总收入
  double listAllCount=0.0;

  //月出库
  int listMonthOut = 0;
  //月入库
  int listMonth = 0;
//  总库存
  int listAll = 0;
  BluetoothDevice? device;
//页面加载完成时
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //设置状态栏颜色
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent, //状态栏背景颜色
        statusBarIconBrightness: Brightness.light // dark:一般显示黑色   light：一般显示白色
    ));
    Future.delayed(Duration.zero,() async {
      //取出个人信息
      permissions = getStorage.read("permissions");
      nickName = json.decode(getStorage.read("User"))['user']['nickName'];
      print("权限:$permissions");
      //获取大数据
      //销售
      if(permissions==0){
        listMonthNum = json.decode(getStorage.read("HomeSalData"))['listMonthNum'];
        listMonthCount = json.decode(getStorage.read("HomeSalData"))['listMonthCount'];
        listAllCount = json.decode(getStorage.read("HomeSalData"))['listAllCount'];
      }
      // 仓管
      if(permissions==1){
        print("仓管，获取仓管数据");
        listMonthOut = json.decode(getStorage.read("HomeCangGuanData"))['listMonthOut'];
        listMonth = json.decode(getStorage.read("HomeCangGuanData"))['listMonth'];
        listAll = json.decode(getStorage.read("HomeCangGuanData"))['listAll'];
      }
      //店长
      if(permissions==2){
        listMonthNum = json.decode(getStorage.read("HomeSalData"))['listMonthNum'];
        listMonthCount = json.decode(getStorage.read("HomeSalData"))['listMonthCount'];
        listAllCount = json.decode(getStorage.read("HomeSalData"))['listAllCount'];
        listMonthOut = json.decode(getStorage.read("HomeCangGuanData"))['listMonthOut'];
        listMonth = json.decode(getStorage.read("HomeCangGuanData"))['listMonth'];
        listAll = json.decode(getStorage.read("HomeCangGuanData"))['listAll'];
      }
    print("月入库:$listMonth");
      //获取连接池
      List<BluetoothDevice>? devideList =await  BleManager.instance.getConnectedDevices();
      if(devideList!.length>0){
        IsConnect = true;
        //获取当前连接的蓝牙设备
        List<BluetoothDevice>? devideList =await  BleManager.instance.getConnectedDevices();
        print("当前连接池设备:$devideList");
        device = devideList![0];
      }else{
        IsConnect = false;
      }
    });
    setState(() {});
  }

  ///页面卸载
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  // TODO: add state variables and methods
  @override
  Widget build(BuildContext context) {
    super.build(context); //要点3
    // TODO: add widget build method
    return Scaffold(
        // extendBodyBehindAppBar: true,
        // appBar: AppBar(
        //
        // ),
        body: Column(
          children: [
            //头部背景图
            Container(

              child: Stack(
                alignment: Alignment.bottomCenter,
                clipBehavior: Clip.none,
                children: [
                  Container(
                    height: 210,
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                            begin: Alignment.topCenter,//渐变开始于上面的中间开始
                            end: Alignment.bottomCenter,//渐变结束于下面的中间
                            colors: [Color(0xff95c1fe), Color(0xff91c4fd),Color(0xff8dcbfe),Color(0xff87d0fb),Color(0xff85d3ff)])
                    ),

                  ),
                  // Image.asset(
                  //   "Image/UserBack.jpg",
                  //   fit: BoxFit.fill,
                  //   width: MediaQuery.of(context).size.width,
                  //   height: 170,
                  // ),
                  // Container(width: MediaQuery.of(context).size.width, height: 220),
                  //头像
                  Positioned(
                    // bottom: 10,
                    top: 70,
                    left: 20,
                    child: Container(
                      width: 70,
                      height: 70,
                      padding: EdgeInsets.all(3),
                      decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.white),
                      child: Container(
                        decoration:
                            BoxDecoration(shape: BoxShape.circle, image: DecorationImage(image: AssetImage('Image/header.jpg'), fit: BoxFit.cover)),
                      ),
                    ),
                  ),
                  Positioned(
                    // bottom: 15,
                    left: 100,
                    top: 100,
                    child: Text(
                      "$nickName",
                      style: TextStyle(fontSize: 20, color: Colors.white),
                    ),
                  ),
                  Positioned(
                    top: 30,
                    child:  Container(
                      child: Text("个人中心",style: TextStyle(color: Colors.white,fontSize: 17),),
                    ),
                  ),
                  Positioned(
                    top: 150,
                    child:  //信息统计 销售
                    permissions == 0
                        ? Container(
                      margin: EdgeInsets.only(top: 15),
                      width: MediaQuery.of(context).size.width - 50,
                      padding: EdgeInsets.all(5),
                      decoration: BoxDecoration(
                        color: Color(0xffffffff),
                        borderRadius: BorderRadius.circular(10.0),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black12,
                              offset: Offset(0.0, 0.0), //阴影x轴偏移量
                              blurRadius: 5, //阴影模糊程度
                              spreadRadius: 1 //阴影扩散程度
                          )],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              // padding: EdgeInsets.only(right: 15),
                              // decoration: BoxDecoration(
                              //     border: Border(right: BorderSide(color: Colors.white,width: 0.5,style: BorderStyle.solid))
                              // ),
                              child: Column(
                                children: [Text("Monthlyorders".tr, style: TextStyle(color: Colors.black54)),
                                  Container(
                                    height: 8,
                                  ),Text("$listMonthNum", style: TextStyle(color: Colors.black54))],
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [
                                  Text("grossincome".tr, style: TextStyle(color: Colors.black54, fontSize: 20)),
                                  Container(
                                    height: 8,
                                  ),
                                  Text("$listAllCount", style: TextStyle(color: Colors.black54,  fontSize: 20))
                                ],
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [
                                  Text(
                                    "Monthlyincome".tr,
                                    style: TextStyle(color: Colors.black54),
                                  ),
                                  Container(
                                    height: 8,
                                  ),
                                  Text("$listMonthCount", style: TextStyle(color: Colors.black54))
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                        : permissions == 1
                        ?
                    //信息统计 仓管、
                    Container(
                      margin: EdgeInsets.only(top: 15),
                      width: MediaQuery.of(context).size.width - 50,
                      padding: EdgeInsets.all(5),
                      decoration: BoxDecoration(
                        color: Color(0xffffffff),
                        borderRadius: BorderRadius.circular(10.0),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black12,
                              offset: Offset(0.0, 0.0), //阴影x轴偏移量
                              blurRadius: 5, //阴影模糊程度
                              spreadRadius: 1 //阴影扩散程度
                          )],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              // padding: EdgeInsets.only(right: 15),
                              // decoration: BoxDecoration(
                              //     border: Border(right: BorderSide(color: Colors.white,width: 0.5,style: BorderStyle.solid))
                              // ),
                              child: Column(
                                children: [Text("Monthlystorage".tr, style: TextStyle(color: Colors.black54)), Container(
                                  height: 8,
                                ),Text("$listMonth", style: TextStyle(color: Colors.black54))],
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [
                                  Text(
                                    "Totalinventory".tr,
                                    style: TextStyle(fontSize: 20, color: Colors.black54),
                                  ),
                                  Container(
                                    height: 8,
                                  ),
                                  Text("$listAll", style: TextStyle( fontSize: 20, color: Colors.black54))
                                ],
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [Text("Monthlyoutofthewarehouse".tr, style: TextStyle(color: Colors.black54)),Container(
                                  height: 8,
                                ), Text("$listMonthOut", style: TextStyle(color: Colors.black54))],
                              ),
                            ),
                          )
                        ],
                      ),
                    )
                        :
                    //信息统计 店长
                    Container(
                      margin: EdgeInsets.only(top: 15),
                      width: MediaQuery.of(context).size.width - 50,
                      padding: EdgeInsets.all(5),
                      decoration: BoxDecoration(
                        color: Color(0xffffffff),
                        borderRadius: BorderRadius.circular(10.0),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black12,
                              offset: Offset(0.0, 0.0), //阴影x轴偏移量
                              blurRadius: 5, //阴影模糊程度
                              spreadRadius: 1 //阴影扩散程度
                          )],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [
                                  Text(
                                    "Totalinventory".tr,
                                    style: TextStyle(color: Colors.black54),
                                  ),
                                  Container(
                                    height: 8,
                                  ),
                                  Text("$listAll", style: TextStyle(color: Colors.black54))
                                ],
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [
                                  Text(
                                    "Totalorders".tr,
                                    style: TextStyle(color: Colors.black54,fontSize: 20),
                                  ),
                                  Container(
                                    height: 8,
                                  ),
                                  Text("$listAll", style: TextStyle(color: Colors.black54))
                                ],
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: 5,right: 5,top: 15,bottom: 10),
                            child: Container(
                              child: Column(
                                children: [Text("grossincome".tr, style: TextStyle(color: Colors.black54)), Container(
                                  height: 8,
                                ),Text("$listAllCount", style: TextStyle(color: Colors.black54))],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),

            //单元格功能
            Container(
              height: 70,
            ),
            //会员管理 --销售--店长
            permissions != 1
                ? Container(
                    child: ListTile(
                      title: Text(
                        "MemberManagement".tr,
                        style: TextStyle(fontSize: 15),
                      ),
                      leading: Icon(Icons.person),
                      trailing: Icon(Icons.keyboard_arrow_right),
                      onTap: () {
                        Get.to(VipList());
                      },
                    ),
                  )
                : Container(),
            Container(
              child: Divider(
                height: 1,
                color: Color(0xfff2f2f2),
                thickness: 1,
              ),
            ),
            //类目管理 --仓管--店长
            permissions != 0
                ? Container(
                    child: ListTile(
                      title: Text(
                        "类目管理",
                        style: TextStyle(fontSize: 15),
                      ),
                      leading: Icon(Icons.view_comfortable),
                      trailing: Icon(Icons.keyboard_arrow_right),
                      onTap: () {
                        Get.to(MaterialTypeManageList());
                      },
                    ),
                  )
                : Container(),
            Container(
              child: Divider(
                height: 1,
                color: Color(0xfff2f2f2),
                thickness: 1,
              ),
            ),
            //员工管理 --店长
            // permissions == 2
            //     ? Container(
            //         child: ListTile(
            //           title: Text(
            //             "员工管理",
            //             style: TextStyle(fontSize: 15),
            //           ),
            //           leading: Icon(Icons.people),
            //           trailing: Icon(Icons.keyboard_arrow_right),
            //           onTap: () {},
            //         ),
            //       )
            //     : Container(),
            // Container(
            //   child: Divider(
            //     height: 1,
            //     color: Color(0xfff2f2f2),
            //     thickness: 1,
            //   ),
            // ),
            //联系我们
            // Container(
            //   child: ListTile(
            //     title: Text(
            //       "联系我们",
            //       style: TextStyle(fontSize: 15),
            //     ),
            //     leading: Icon(Icons.phone),
            //     trailing: Icon(Icons.keyboard_arrow_right),
            //     onTap: (){
            //
            //     },
            //   ),
            // ),
            // Container(
            //   child: Divider(
            //     height: 1,
            //     color: Color(0xfff2f2f2),
            //     thickness: 1,
            //   ),
            // ),
            //退出账号
            //退出账号
            Container(
              child: ListTile(
                title: Text(
                  "Signoutofyouraccount".tr,
                  style: TextStyle(fontSize: 15),
                ),
                leading: Icon(Icons.exit_to_app),
                trailing: Icon(Icons.keyboard_arrow_right),
                onTap: () {
                  //清空本地缓存，前往登录页面
                  Get.defaultDialog(
                      title: "prompt".tr,
                      middleText: "Areyousureyouwanttologoutofyouraccount".tr,
                      barrierDismissible: false,
                      confirm: MaterialButton(
                          onPressed: () async {
                            // Get.back();
                            //清空本地缓存
                            await getStorage.remove("User");
                            await getStorage.remove("permissions");
                            if(IsConnect){
                              //断开连接
                              DisConnect();
                            }
                            Get.offAll(Login());
                          },
                          child: Text("sure".tr,style: TextStyle(color: Colors.white),),
                      color: Color(0xff372D9F),
                      ),
                      cancel: MaterialButton(
                          onPressed: () {
                            Get.back();
                          },
                          child: Text(
                            "Cancel".tr,
                            style: TextStyle(color: Color(0xff372D9F)),
                          ),
                        color: Colors.white,
                        //边框样式
                        shape: const RoundedRectangleBorder(
                          //边框颜色
                          side: BorderSide(
                            color: Color(0xff372D9F),
                            width: 1,
                          ),
                        ),
                      ));
                },
              ),
            ),
            Container(
              child: Divider(
                height: 1,
                color: Color(0xfff2f2f2),
                thickness: 1,
              ),
            ),
          ],
        ));
  }
  //  断开蓝牙
  void DisConnect() {
    if(IsConnect){
      BleManager.instance.disconnectDevice(device!).then((value) {
        print("disconnected !!");
        getStorage.remove("IsConnect");
      });
    }

  }
}
